package edu.npu.cv.utils;

import org.apache.commons.lang3.StringUtils;

public class CommonUtils {
    /**
     * 邮箱地址正则
     */
    private static final String EMAIL_REGEX =
            "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    /**
     * 中国大陆手机号正则
     */
    private static final String CHINA_MAINLAND_MOBILE_PHONE_REGEX = "0?(13|14|15|17|18|19)[0-9]{9}";

    /**
     * 只包含中英文和数字的字符串正则
     */
    private static final String STRING_ONLY_CONTAIN_LETTER_OR_NUMBER_REGEX = "^[a-zA-Z0-9\\u4E00-\\u9FA5]+$";

    /**
     * 网址正则
     */
    private static final String URL_REGEX =
            "^([hH][tT]{2}[pP]:/*|[hH][tT]{2}[pP][sS]:/*|[fF][tT][pP]:/*)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\\\/])+(\\\\?{0,1}(([A-Za-z0-9-~]+\\\\={0,1})([A-Za-z0-9-~]*)\\\\&{0,1})*)$";

    /**
     * 判断是否是邮箱地址
     *
     * @param input
     * @return
     */
    public static boolean isEmailAddress(String input) {
        if (StringUtils.isBlank(input)) {
            return false;
        }
        return input.matches(EMAIL_REGEX);
    }

    /**
     * 判断是否是手机号
     * <p>
     * 目前只支持中国大陆手机号
     * </p>
     *
     * @param input
     * @return
     */
    public static boolean isMobliePhoneNumber(String input) {
        if (StringUtils.isBlank(input)) {
            return false;
        }
        return input.matches(CHINA_MAINLAND_MOBILE_PHONE_REGEX);
    }

    /**
     * 验证只包含中英文和数字的字符串
     *
     * @param keyword
     * @return
     */
    public static boolean validKeyword(String keyword) {
        if (StringUtils.isBlank(keyword)) {
            return false;
        }
        return keyword.matches(STRING_ONLY_CONTAIN_LETTER_OR_NUMBER_REGEX);
    }

    /**
     * 判断是否是网址
     *
     * @param urlString
     * @return
     */
    public static boolean isURL(String urlString) {
        if (StringUtils.isBlank(urlString)) {
            return false;
        }
        return urlString.matches(URL_REGEX);
    }
}
